package com.example.demoamqp.config;

import com.example.demoamqp.conatants.Constants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 创建消息队列并注入容器中
 *
 * @author ross
 */

@Configuration
public class QueueConfig {

    /**
     * 创建队列
     *
     * @return
     */
    @Bean
    public Queue createQueue() {
        return new Queue("ross_amq");
    }


    /******************************  发布、订阅者模式  *********************************/
    @Bean // 邮箱的队列
    public Queue mailQueue(){
        return new Queue(Constants.MQ_MAIL_QUEUE,
                Constants.durable,
                Constants.exclusive,
                Constants.autoDelete);
    }

    @Bean // 电话的队列
    public Queue phoneQueue(){
        return new Queue(Constants.MQ_PHONE_QUEUE,
                Constants.durable,
                Constants.exclusive,
                Constants.autoDelete);
    }
    @Bean // 交换机
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(Constants.MQ_FANOUT_EXCHANGE,
                Constants.durable,
                Constants.autoDelete);
    }

    /**
     * 邮箱绑定交换机
     * @return
     */
    @Bean
    public Binding mailBinding(){
        return BindingBuilder.bind(mailQueue())
                .to(fanoutExchange());
    }

    /**
     * 电话绑定交换机
     * @return
     */
    @Bean
    public Binding phoneBinding(){
        return BindingBuilder.bind(phoneQueue())
                .to(fanoutExchange());
    }




    /*----------------------------------------------------*/
    @Bean // A队列
    public Queue topicAQueue(){
        return new Queue(Constants.MQ_TOPIC_QUEUE_A,
                Constants.durable,
                Constants.exclusive,
                Constants.autoDelete);
    }

    /**
     * topic模式相关配置
     */

    @Bean // B队列
    public Queue topicBQueue(){
        return new Queue(Constants.MQ_TOPIC_QUEUE_B,
                Constants.durable,
                Constants.exclusive,
                Constants.autoDelete);
    }

    @Bean // topic的交换机
    public TopicExchange topicMyExchange(){
        return new TopicExchange(Constants.MQ_TOPIC_EXCHANGE,
                Constants.durable,
                Constants.autoDelete);
    }


    @Bean
    public Binding topicAQueueBinding(){
        return BindingBuilder
                .bind(topicAQueue())
                .to(topicMyExchange())
                .with("topic.xxx"); // 规则 topic.xxx
    }

    @Bean
    public Binding topicBQueueBinding(){
        return BindingBuilder
                .bind(topicBQueue())
                .to(topicMyExchange())
                .with("topic.*"); // 规则 topic.xxx
    }
}